package scsys.servlet;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import scsys.mapper.CourseMapper;
import scsys.pojo.Course;
import scsys.util.SqlSessionFactoryUtils;

import java.util.List;

public class CourseServiceImpl implements CourseService {
    //1. 创建SqlSessionFactory 工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();


    @Override
    public void add(Course course) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        //4. 调用方法
        mapper.add(course);
        // 5、提交事務
        sqlSession.commit();
        //5. 释放资源
        sqlSession.close();
    }

    @Override
    public List<Course> selectMore(Course course) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        //4. 调用方法
        List<Course> courses = null;
        courses = mapper.selectMore(course);

        //5. 释放资源
        sqlSession.close();
        return courses;
    }

    @Override
    public Course selectOne(Course course) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        //4. 调用方法
        Course selectCourse = null;
        selectCourse = mapper.selectOne(course);

        //5. 释放资源
        sqlSession.close();

        return selectCourse;
    }

    @Override
    public List<Course> selectAll() {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        //4. 调用方法
        List<Course> courses = null;
        courses = mapper.selectAll();

        //5. 释放资源
        sqlSession.close();
        return courses;
    }

    @Override
    public void deleteCourse(Course course) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        //4. 调用方法
        mapper.deleteCourse(course);
        // 5、提交事務
        sqlSession.commit();
        //5. 释放资源
        sqlSession.close();
    }

}
